package com.huawei.statistics.controller.file;

import com.huawei.statistics.controller.common.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Iterator;

/**
 * 
 * 文件上传
 * @author ywx
 *
 */


@Controller
@RequestMapping("admin/file")
public class FileUpLoad extends AbstractController {
	
	@Autowired
	HttpSession session;
	@Autowired
	HttpServletRequest request;
	
	

	@GetMapping("/test")
	public String test(HttpSession httpSession){
		System.out.println("输出用户信息");
		System.out.println(httpSession.getAttribute("userId"));
		return "file/file";
	}

	@GetMapping("/delFile")
	@ResponseBody
	public Boolean delFile(@RequestParam String path){

		path = request.getSession().getServletContext().getRealPath("")+"/"+path;
		System.out.println(path);
		File file = new File(path);
		if(!file.exists()){
			return false;
		}
		file.delete();
		return  true;

	}


	//获取网络图片
	//@GetMapping("/saveNetFile")
	@RequestMapping(value = "/saveNetFile", method= RequestMethod.GET)
	@ResponseBody
	public String savaNetFile(@RequestParam String fileUrl){
		 //fileUrl ="http://www.zgnz168.com/uploadfile/201711/ac08105814.jpg";
		System.out.println(fileUrl);
		String prefix=fileUrl.substring(fileUrl.lastIndexOf(".")+1);
		String path = request.getSession().getServletContext().getRealPath("");
		File file = null;
		try {
			URL url = new URL(fileUrl);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.setRequestMethod("GET");
			connection.setReadTimeout(5*1000);
			InputStream is = connection.getInputStream();
			byte[] data = new byte[1024];
			int index ;
			file = new File(path+"/img/"+new Date().getTime()+"."+prefix);
			if(!file.exists()){
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			path =file.getAbsolutePath();
			System.out.println("file create success");
			OutputStream os = new FileOutputStream(file);
			while((index=is.read(data))!=-1){
				os.write(data,0,index);
			}
			is.close();
			os.close();
			System.out.println(file.getAbsolutePath());
		} catch (Exception e) {

			e.printStackTrace();
			return "文件路径错误";
		}
		System.out.println(file.getAbsoluteFile());
		return "img/"+file.getName();

	}








	
	 @RequestMapping("/img")
	 @ResponseBody
	 public String  springUpload(HttpServletRequest request) throws IllegalStateException, IOException{
	       long  startTime=System.currentTimeMillis();
	       CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
	       File tarFile = null;
	        if(multipartResolver.isMultipart(request)){
	            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
	            Iterator iter=multiRequest.getFileNames();
	            while(iter.hasNext()){
	                MultipartFile file=multiRequest.getFile(iter.next().toString());
	                if(file!=null){
	                String path = request.getSession().getServletContext().getRealPath("");
	                    System.out.println(path);
	                     tarFile = new File(path+"image"+new Date().getTime()+file.getOriginalFilename());
	                    
	                     if(!tarFile.exists()){
	                    	 tarFile.mkdirs();
	                     }
	                 file.transferTo(tarFile);
	                }
	            }
	        }
	        long  endTime=System.currentTimeMillis();
	        System.out.println("上传时间："+String.valueOf(endTime-startTime)+"ms");
	        return tarFile.getPath(); 
	    }
	 
	 
	 
	 
	   
   
   

}
